package process;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import entity.Step;

public class StepProcess extends EntityProcess {
	public Collection<Step> getStepsByCase(long id)throws Exception{
		EntityManager em = this.getEntityManager();
		try{			
			Query q = em.createQuery("SELECT p FROM "+Step.class.getName()+" p WHERE P.caseId=?1");
			q.setParameter(1, id);
			
		    @SuppressWarnings("unchecked")
			List<Step> results = q.getResultList();
		    
		    for (Step step : results){
		    	if (step.getRequest() != null) {
		    		int length = step.getRequest().length();
		    		System.out.println(length);
		    	}
		    	if (step.getResponse() != null) {
		    		int length = step.getResponse().length();
		    		System.out.println(length);
		    	}
				//em.detach(giant.getData());
				em.detach(step);
		    }
		    
			return results;
		}
		catch(Exception ex){
			throw ex;//this.LogException(ex);
		}
		finally{
			this.releaseEntityManager(em);
		}
	}
	
	
	public void saveSteps(Long caseId, Collection<Step> entities)throws Exception{
		Collection<Step> exists = this.getStepsByCase(caseId);
		this.saveEntities(caseId, entities, exists);
		/*for(Step step: entities){
			this.getEntityManager().detach(step.getRequest());
		}*/
	}
}
